﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Utils;

namespace UI.Desktop.Comisiones
{
    public partial class Register : Form
    {
        private Entities.Comision _current_instance = new Entities.Comision();
        private Logic.Carreras _hnd_carreras = new Logic.Carreras();
        private Logic.Planes _hnd_planes = new Logic.Planes();
        private Logic.Comisiones _hnd_comisiones = new Logic.Comisiones();
        private bool _need_refresh = false;
        private CommonControls.FormMode _form_mode = CommonControls.FormMode.New;

        private Entities.Comision CurrentInstance
        {
            get { return _current_instance; }
            set { _current_instance = value; }
        }
        private Logic.Carreras HndCarreras
        {
            get { return _hnd_carreras; }
        }
        private Logic.Planes HndPlanes
        {
            get { return _hnd_planes; }
        }
        private Logic.Comisiones HndComisiones
        {
            get { return _hnd_comisiones; }
        }
        private bool NeedRefresh
        {
            get { return _need_refresh; }
            set { _need_refresh = value; }
        }
        private CommonControls.FormMode FormMode
        {
            get { return _form_mode; }
            set { _form_mode = value; }
        }

        public Register()
        {
            InitializeComponent();
            InitializeControl();
            Text += "Agregar";
        }

        public Register(Entities.Comision instance)
        {
            InitializeComponent();
            InitializeControl();
            FormMode = CommonControls.FormMode.Modify;
            Text += "Editar";
            CurrentInstance = instance;
            CurrentInstance.State = Commons.EntityState.Modified;
            txtNombre.Text = CurrentInstance.Nombre;
            txtAnoEspecialidad.Text = CurrentInstance.AnoEspecialidad.ToString();
            cmbCarrera.SelectedValue = HndCarreras.GetByID(HndPlanes.GetByID(CurrentInstance.IDPlan).IDCarrera).ID;
            cmbPlan.SelectedValue = CurrentInstance.IDPlan;
        }

        private void InitializeControl()
        {
            txtAnoEspecialidad.KeyPress += CommonControls.NumberTextBox_KeyPress;
            cmbCarrera.SelectedIndexChanged -= cmbCarrera_SelectedIndexChanged;
            CommonControls.CarrerasToCombo(ref cmbCarrera);
            cmbCarrera.SelectedIndex = -1;
            cmbCarrera.SelectedIndexChanged += cmbCarrera_SelectedIndexChanged;
        }

        private string Save()
        {
            CurrentInstance.Nombre = txtNombre.Text;
            CurrentInstance.AnoEspecialidad = (txtAnoEspecialidad.Text.Trim().IsEmptyString() ? -1 : txtAnoEspecialidad.Text.Trim().ToInt32());
            CurrentInstance.IDPlan = (cmbPlan.SelectedIndex != -1 ? cmbPlan.SelectedValue.ToInt32() : -1);

            return HndComisiones.Save(CurrentInstance);
        }

        private void btnAceptar_Click(object sender, EventArgs e)
        {
            string r = Save();

            if (!r.IsEmptyString()) CommonControls.ErrorBox(Text, r);
            else
            {
                NeedRefresh = true;
                if (FormMode == CommonControls.FormMode.New)
                {
                    if (CommonControls.AskBox(Text, "Nueva comisión agregada exitosamente.\n¿Desea agregar otra más?") == DialogResult.Yes)
                    {
                        CommonControls.ClearControls(ref pnlMain);
                        CurrentInstance = new Entities.Comision();
                        txtNombre.Focus();
                    }
                    else Dispose();
                }
                else Dispose();
            }
        }

        private void Register_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult = (NeedRefresh ? DialogResult.OK : DialogResult.Cancel);
        }

        private void cmbCarrera_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbCarrera.SelectedIndex != -1)
            {
                Entities.Carrera c = HndCarreras.GetByID(cmbCarrera.SelectedValue.ToInt32());
                CommonControls.PlanesToCombo(ref cmbPlan, c);
            }
        }
    }
}
